package com.atguigu.flink.chapter11;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/9/24 10:10
 */
public class Flink06_Sql_File {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);
        
        StreamTableEnvironment tenv = StreamTableEnvironment.create(env);
        
        // 用ddl的方式, 直接建表去关联source
        tenv.executeSql("create table sensor(" +
                            "   id string, " +
                            "   ts bigint, " +
                            "   vc int " +
                            ")with(" +
                            "   'connector' = 'filesystem', " +
                            "   'path' = 'input/sensor.txt', " +
                            "   'format' = 'csv' " +
                            ")");
        
        
//        tenv.sqlQuery("select * from sensor where id = 'sensor_1'").execute().print();
        
        // 用ddl的方式, 直接建表去关联sink
        tenv.executeSql("create table sink_sensor(" +
                            "   id string, " +
                            "   ts bigint, " +
                            "   vc int " +
                            ")with(" +
                            "   'connector' = 'filesystem', " +
                            "   'path' = 'input/aaa', " +
                            "   'format' = 'csv' " +
                            ")");
    
        /*Table table = tenv.sqlQuery("select * from sensor where id = 'sensor_1'");
        table.executeInsert("sink_sensor");*/
    
        tenv.executeSql("insert into sink_sensor select * from sensor where id = 'sensor_1'");
    
    }
}
